﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using VNWIKI_WEB;
using VNWIKI_WEB.Models;

namespace VNWIKI_WEB.Controllers
{ 
    public class AlbumController : Controller
    {
        VNWIKIDataContext db = new VNWIKIDataContext();

        //
        // GET: /Album/

        public ActionResult Index()
        {
            User admin = (User)Session["user"];
            if (admin == null || admin.Admin == false)
                return RedirectToAction("LogOn", "Admin");

            var albums = db.Albums.Include(a => a.Category);
            return View(albums.ToList());
        }

        //
        // GET: /Album/Details/5

        public ActionResult Details(int id)
        {
            User admin = (User)Session["user"];
            if (admin == null || admin.Admin == false)
                return RedirectToAction("LogOn", "Admin");

            Album album = db.Albums.Single(u=> u.ID == id);
            return View(album);
        }

        //
        // GET: /Album/Create

        public ActionResult Create()
        {
            User admin = (User)Session["user"];
            if (admin == null || admin.Admin == false)
                return RedirectToAction("LogOn", "Admin");

            ViewBag.CategoryID = new SelectList(db.Categories, "ID", "Name");
            return View();
        } 

        //
        // POST: /Album/Create

        [HttpPost]
        public ActionResult Create(Album album)
        {
            if (ModelState.IsValid)
            {
                db.Albums.InsertOnSubmit(album);
                db.SubmitChanges();
                return RedirectToAction("Index");  
            }

            ViewBag.CategoryID = new SelectList(db.Categories, "ID", "Name", album.CategoryID);
            return View(album);
        }
        
        //
        // GET: /Album/Edit/5
 
        public ActionResult Edit(int id)
        {
            User admin = (User)Session["user"];
            if (admin == null || admin.Admin == false)
                return RedirectToAction("LogOn", "Admin");

            Album album = db.Albums.Single(u => u.ID == id);
            ViewBag.CategoryID = new SelectList(db.Categories, "ID", "Name", album.CategoryID);
            return View(album);
        }

        //
        // POST: /Album/Edit/5

        [HttpPost]
        public ActionResult Edit(Album album)
        {
            if (ModelState.IsValid)
            {
                Album alb = db.Albums.Single(u => u.ID == album.ID);
                alb.Name = album.Name;
                alb.Photos = album.Photos;
                alb.Tags = album.Tags;
                alb.UserID = album.UserID;
                alb.CategoryID = album.CategoryID;
                alb.Views = album.Views;
                alb.Vote = album.Vote;
                db.SubmitChanges();
                return RedirectToAction("Index");
            }
            ViewBag.CategoryID = new SelectList(db.Categories, "ID", "Name", album.CategoryID);
            return View(album);
        }

        //
        // GET: /Album/Delete/5
 
        public ActionResult Delete(int id)
        {
            User admin = (User)Session["user"];
            if (admin == null || admin.Admin == false)
                return RedirectToAction("LogOn", "Admin");

            Album album = db.Albums.Single(u => u.ID == id);
            return View(album);
        }

        //
        // POST: /Album/Delete/5

        [HttpPost, ActionName("Delete")]
        public ActionResult DeleteConfirmed(int id)
        {
            Album album = db.Albums.Single(u => u.ID == id);
            
            List<Photo> photoList;
            photoList = (from c in db.Photos
                         where c.AlbumID.Equals(id)
                         select c).ToList();
            foreach(var p in photoList){
                db.Photos.DeleteOnSubmit(p);
            }

            db.Albums.DeleteOnSubmit(album);
            db.SubmitChanges();
            return RedirectToAction("Index");
        }

        protected override void Dispose(bool disposing)
        {
            db.Dispose();
            base.Dispose(disposing);
        }
    }
}